package com.xzz.stream;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author: hhz
 * @createDate: 2021-4-23
 */
public class StreamTest {
    public static void main(String[] args) {
        List<Person> personList = new ArrayList<Person>();
        personList.add(new Person("Tom", 8900, 11, "male", "New York"));
        personList.add(new Person("Jack", 7800, 12, "male", "Washington"));
        personList.add(new Person("Lily", 7800, 13, "female", "Washington"));
        personList.add(new Person("Anni", 8200, 14, "female", "New York"));
        personList.add(new Person("Owen", 9500, 12, "male", "New York"));
        personList.add(new Person("Alisa", 7900, 20, "female", "New York"));
        personList.add(new Person("Aa", 7900, 21, "female", "New York"));

        List<String> filterList = personList.stream().filter(x -> x.getSalary() > 8000).map(Person::getName)
                .collect(Collectors.toList());
        System.out.print("高于8000的员工姓名：" + filterList);
        System.out.println();
        Optional<Person> max = personList.stream().max(Comparator.comparing(Person::getAge));
        System.out.println("年紀最大的：" + max.get().getName());
        Optional<Person> min = personList.stream().max((o1, o2) -> o2.getAge() - o1.getAge());
        System.out.println("年紀最小的：" + min.get().getName());


        List<Person> collect = personList.stream().filter(person -> person.getSalary() > 8000).collect(Collectors.toList());
        System.out.print("筛选出salary大于8000的员工 :");
        collect.forEach(x -> System.out.print(x.getName() + " "));
        System.out.println();
        System.out.print("员工的最高薪资 :");
        personList.stream().max(Comparator.comparing(Person::getSalary)).ifPresent(x -> System.out.print(x.getSalary()));
        System.out.println();
        System.out.print("员工的薪资之和 :");
        personList.stream().map(Person::getSalary).reduce(Integer::sum).ifPresent(System.out::print);
        System.out.println();
        System.out.print("员工的人数 :"+ personList.size());
        System.out.println();
        System.out.print("员工的平均薪资 :"+personList.stream().map(Person::getSalary).collect(Collectors.averagingInt(x -> x)));
        System.out.println();
        System.out.print("员工的平均薪资 :"+personList.stream().collect(Collectors.averagingInt(Person::getSalary)));
        System.out.println();
        System.out.println("将员工按薪资从高到低排序，同样薪资者年龄小者在前 :");
        personList.stream().sorted(Comparator.comparingInt(Person::getSalary).reversed().thenComparing(Person::getAge))
                .forEach(x-> System.out.print(x.getName()+"："+x.getSalary()+" "+x.getAge()+"   "));
        System.out.println();
        personList.stream().sorted((a,b)->{
            if(a.getSalary()==b.getSalary()){
                return a.getAge()-b.getAge();
            }else {
                return b.getSalary()-a.getSalary();
            }
        }).forEach(x-> System.out.print(x.getName()+"："+x.getSalary()+" "+x.getAge()+"   "));
        System.out.println();
        System.out.println("将员工按性别分类:");
        Map<String, List<Person>> collect1 = personList.stream().collect(Collectors.groupingBy(Person::getSex));
        collect1.forEach((key, value) -> {
            System.out.print("性别：" + key+" :");
            value.forEach(y -> System.out.print(y.getName() + " "));
            System.out.println();
        });
        System.out.println("将员工按性别和地区分类:");
        Map<String, Map<String, List<Person>>> collect2 = personList.stream().collect(Collectors.groupingBy(Person::getArea, Collectors.groupingBy(Person::getSex)));
        collect2.forEach((key, value) -> {
            System.out.print("地区：" + key+" :");
            System.out.println();
            value.forEach((a, b) ->{
                System.out.print("  性别：" + a+" :");
                b.forEach(y -> System.out.print(y.getName() + " "));
                System.out.println();
            });
        });
        System.out.println();
        System.out.println("将员工按薪资是否高于8000分为两部分:");
        Map<Boolean, List<Person>> collect3 = personList.stream().collect(Collectors.partitioningBy(x -> x.getSalary() > 8000));
        collect3.forEach((key, value) -> {
            System.out.print("是否高于8000：" + (key?"是":"否")+" :");
            value.forEach(y -> System.out.print(y.getName() + " "));
            System.out.println();
        });
        System.out.println();
        String names = personList.stream().map(Person::getName).collect(Collectors.joining(","));
        System.out.println("所有员工的姓名：" + names);

    }
}
